package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    //新增分类
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .type(categoryDTO.getType())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .status(StatusConstant.ENABLE)
                .build();
        categoryMapper.addCategory(category);
    }

    //    分类分页查询
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = (Page<Category>) categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //    修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .id(categoryDTO.getId())
                .type(categoryDTO.getType())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .build();
        categoryMapper.updateCategory(category);
    }

    //    删除分类
    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    //    根据id查询分类
    @Override
    public Category getById(Long id) {
        return categoryMapper.getById(id);
    }

    //    启用、禁用分类
    @Override
    public void updateStatus(Integer status, Long id) {
        categoryMapper.updateStatus(status, id);
    }

    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.getByType (type);
    }
    @Override
    public List<Category> list() {
        return categoryMapper.getAll();
    }
}
